{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parse and Format a Variant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SequenceVariant(ac=NC_000001.10, type=g, posedit=150550916_150550920delGACAAinsCAATACC)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hgvs.parser\n",
    "hp = hgvs.parser.Parser()\n",
    "\n",
    "g_hgvs = 'NC_000001.10:g.150550916_150550920delGACAAinsCAATACC'\n",
    "g_var = hp.parse_hgvs_variant(g_hgvs)\n",
    "g_var"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Map variants between sequences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:hgvs.dataproviders.uta:connected to postgresql://anonymous:anonymous@localhost/uta_dev/uta_20150827\n"
     ]
    }
   ],
   "source": [
    "import hgvs.dataproviders.uta\n",
    "hdp = hgvs.dataproviders.uta.connect()\n",
    "\n",
    "import hgvs.variantmapper\n",
    "am = hgvs.variantmapper.AssemblyMapper(hdp, assembly_name='GRCh37', alt_aln_method='splign')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "c_vars = [am.g_to_c(g_var, ac) for ac in am.relevant_transcripts(g_var)]\n",
    "c_vars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p_vars = [am.c_to_p(c_var) for c_var in c_vars]\n",
    "p_vars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Normalize (Shuffle and Rewrite) Variants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import hgvs.normalizer\n",
    "hn = hgvs.normalizer.Normalizer(hdp)\n",
    "hn.normalize(hp.parse_hgvs_variant('NM_021960.4:c.735_736insT'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Validate Variants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import hgvs.validator\n",
    "hv = hgvs.validator.Validator(hdp)\n",
    "try:\n",
    "    hv.validate(hp.parse_hgvs_variant('NM_021960.4:c.736_740delATGTCinsGGTATTG'))\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `SequenceVariant` instances are structured variant representations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "g_var.ac, g_var.type, g_var.posedit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "g_var.posedit.pos.start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "g_var.posedit.edit.ref, g_var.posedit.edit.alt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Format variants simply by \"stringifying\" them with `print` or `format`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"\\n\".join([\"Your variant was {v} mapped to:\".format(v=g_var)]\n",
    "                + [\"  {c_var} ({p_var})\".format(c_var=c_var, p_var=p_var)\n",
    "                   for c_var,p_var in zip(c_vars,p_vars)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NCBI SeqViewer for MCL1 region on GRCh37.p13\n",
    "<img src=\"images/MCL1.png\">\n",
    "<img src=\"images/MCL1-zoom.png\">"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
